<?php
global $_W, $_GPC;
$uniacid = $_W['uniacid'];
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ('post' == $op) {
	$cateall = pdo_fetchall("SELECT * FROM " . tablename($this->tablecatedtk) . " WHERE uniacid = '{$uniacid}' AND status=1  order by displayorder desc,id desc");
	$id = intval($_GPC['id']);
	if (!empty($id)) {
		$item = pdo_fetch("SELECT * FROM " . tablename($this->tableitemdtk) . " WHERE uniacid=:uniacid AND id = :id", array(':uniacid' => $uniacid, ':id' => $id));
		if (empty($item)) {
			message('数据不存在！', '', 'error');
		}
	}
	if (checksubmit('submit')) {
		$title = trim($_GPC['title']);
		if (empty($title)) {
			message('商品名称不能为空!');
		}
		$data = array('uniacid' => $_W['uniacid'], 'displayorder' => $_GPC['displayorder'], 'itemid' => $_GPC['itemid'], 'title' => $_GPC['title'], 'itempic' => $_GPC['itempic'], 'shoptitle' => $_GPC['shoptitle'], 'itemfee' => $_GPC['itemfee'], 'itemmsell' => $_GPC['itemmsell'], 'itemstatus' => $_GPC['itemstatus'], 'stime' => $_GPC['stime'], 'etime' => $_GPC['etime'], 'itemsurl' => $_GPC['itemsurl'], 'itemurl' => $_GPC['itemurl'], 'itemtkl' => $_GPC['itemtkl'], 'itemyhj_zl' => $_GPC['itemyhj_zl'], 'itemyhj_yl' => $_GPC['itemyhj_yl'], 'itemyhj_tit' => $_GPC['itemyhj_tit'], 'itemyhj_stime' => $_GPC['itemyhj_stime'], 'itemyhj_etime' => $_GPC['itemyhj_etime'], 'itemyhj_url' => $_GPC['itemyhj_url'], 'itemyhj_tkl' => $_GPC['itemyhj_tkl'], 'status' => $_GPC['status'], 'cate_id' => $_GPC['cate_id'], 'quan_link' => $_GPC['quan_link'], 'atime' => TIMESTAMP,);
		if (empty($id)) {
			pdo_insert($this->modulename . '_dtk_item', $data);
			message('商品添加成功！', $this->createWebUrl('listitemdtk', array('op' => 'display')), 'success');
		} else {
			pdo_update($this->modulename . '_dtk_item', $data, array('id' => $id));
			message('商品更新成功！', $this->createWebUrl('listitemdtk', array('op' => 'display')), 'success');
		}
	}
} else if ('del' == $op) {
	$id = intval($_GPC['id']);
	$row = pdo_fetch("SELECT id FROM " . tablename($this->tableitemdtk) . " WHERE uniacid=:uniacid AND id = :id", array(':uniacid' => $uniacid, ':id' => $id));
	if (empty($row)) {
		message('商品不存在！');
	}
	pdo_delete($this->modulename . '_dtk_item', array('id' => $id));
	message('删除成功！', referer(), 'success');
} else if ('delall' == $op) {
	$ids = $_GPC['ids'];
	foreach ($ids as $id) {
		pdo_delete($this->modulename . '_dtk_item', array('id' => $id));
		pdo_delete($this->modulename . '_fav', array('itemid' => $id));
	}
	message('删除' . $id . '成功！', referer(), 'success');
} else if ('display' == $op) {
	if (checksubmit('submit')) {
		$displayorders = $_GPC['displayorder'];
		foreach ($displayorders as $id => $displayorder) {
			pdo_update($this->modulename . '_dtk_item', array('displayorder' => $displayorder), array('id' => $id));
		}
		message('批量更改排序成功', referer(), 'success');
	}
	$cateall = pdo_fetchall("SELECT * FROM " . tablename($this->tablecatedtk) . " WHERE uniacid = '{$uniacid}' AND status=1 order by displayorder desc,id desc");
	$condition1 = "";
	$condition2 = "";
	$zhuangtai = "";
	$condition4 = "";
	if (!empty($_GPC['keyword'])) {
		$condition1 .= " AND CONCAT(`title`,`itemid`) LIKE '%{$_GPC['keyword']}%'";
	}
	if (!empty($_GPC['cate_id'])) {
		$condition2 .= " AND CONCAT(`cate_id`) = '{$_GPC['cate_id']}'";
	}
	if (!empty($_GPC['id'])) {
		$condition4 .= " AND CONCAT(`id`) = '{$_GPC['id']}'";
	}
	if (!empty($_GPC['zhuangtai'])) {
		if ($_GPC['zhuangtai'] == 0) {
			$zhuangtai .= "";
		}
		if ($_GPC['zhuangtai'] == 1) {
			$zhuangtai .= " AND CONCAT(`itemyhj_url`) = '' ";
		}
		if ($_GPC['zhuangtai'] == 2) {
			$zhuangtai .= " AND CONCAT(`status`) = '0' ";
		}
		if ($_GPC['zhuangtai'] == 3) {
			$zhuangtai2 .= " hit desc,";
		}
		if ($_GPC['zhuangtai'] == 4) {
			$zhuangtai2 .= " share desc,";
		}
	}
	$pindex = max(1, intval($_GPC['page']));
	if ($_GPC['psize']) {
		$psize = $_GPC['psize'];
	} else {
		$psize = 10;
	}
	$list = pdo_fetchall("SELECT * FROM " . tablename($this->tableitemdtk) . " WHERE uniacid = '{$_W['uniacid']}' $condition1 $condition2 $condition4 $zhuangtai order by $zhuangtai2 displayorder desc,id desc,atime desc LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
	if ($list) {
		foreach ($list as $item => $xfgw) {
			$cate = pdo_fetch("SELECT * FROM " . tablename($this->tablecatedtk) . " WHERE id = '{$list[$item]['cate_id']}'");
			$list[$item]['catename'] = $cate['name'];
		}
	}
	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->tableitemdtk) . " WHERE uniacid = '{$_W['uniacid']}' $condition1 $condition2 $condition4 $zhuangtai");
	$total_none = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->tableitemdtk) . " WHERE uniacid = '{$_W['uniacid']}' $condition1 $condition2 $condition4 AND CONCAT(`itemyhj_url`) = ''");
	$pager = pagination($total, $pindex, $psize);
} else if ('status' == $op) {
	$id = $_GPC['id'];
	pdo_query('update ' . tablename($this->tableitemdtk) . " set status = !status where id='{$id}'");
	die('1');
}
include $this->template('listitemdtk');